import { observable, action, computed } from 'mobx'
import { GroupList, GroupItem, GroupMemberList, GroupAllMember, GroupMemItem, GroupMemRole, VirualGroupItem } from '../interface/IGroup';
import imsdk from '../net/IMSDK';
// import message from 'antd/es/message';
import chatStore from './ChatStore';
import { FriendItem } from '../interface/IFriend';
import systemStore from './SystemStore';
import webIM from '../net/WebIM';
import { createGroupStore } from '../store/CreatGroupStore';

/**
 * 用户标签组，基于标签对用户进行分组
 */
export class TagGroupStore {

    _groupList: VirualGroupItem[] = [];
    groupList = observable(this._groupList);
    // 所有群成员列表数据
    _groupMemberList: GroupMemberList = new Map();
    @observable groupMemberList = observable(this._groupMemberList);

    @observable selectGroupId: string = '';


    @action setGroupList = (groups: VirualGroupItem[]) => {
        this.groupList.replace(groups)
    }

    @action createTagGroup = async (groupName: string) =>{
        chatStore.currentChatData.tagGroups = [...chatStore.currentChatData.tagGroups, new VirualGroupItem({
            name:groupName,
        })]
        let res = await imsdk.createTagGroup(groupName);
        if( res && res.resultCode == 1){
            webIM.loadTagGroupList()
        }
    }

    @action addTagGroup = async (userId:string, groupName: string) =>{
        chatStore.currentChatData.tagGroups = [...chatStore.currentChatData.tagGroups, new VirualGroupItem({
            name:groupName,
        })]
        let res = await imsdk.createTagGroup(groupName);
        if( res && res.resultCode == 1){
            res= await imsdk.setTagGroupUser(groupName, userId);
        }
        webIM.loadTagGroupList()
    }

    
    /**
     * 将用户加至组
     * @param userIds 
     * @param groupName 
     */
    @action addUsersTag = async( userIds:string[], groupName: string) =>{
        for(let id of userIds){
            let res = await imsdk.setTagGroupUser(groupName, id);
        }
        webIM.loadTagGroupList()
    }
    /** 批量更新用户标签 */
    @action updateUsersTags = async (userIds: string[],oldUserIds:any[], groupNames:string, sync:boolean = false) =>{
        if( sync ){
            // db
            let delGroups = oldUserIds.filter(item=>!userIds.includes(item.toUserId+''))
            console.log('delete group', delGroups)
            for (let i = 0; i < delGroups.length; i++) {
                let res = await imsdk.removeTagGroupUser(groupNames, delGroups[i].toUserId);
            }
            let addGroups = userIds.filter(item=>!oldUserIds.find(u=> u.toUserId+'' == item))
            for (let i = 0; i < addGroups.length; i++) {
                let res = await imsdk.setTagGroupUser(groupNames, addGroups[i]);
            }
            webIM.loadTagGroupList();
        }
        
        
    }

    @action updateTagGroup = async (userId: string, groupNames: string[], sync:boolean = false) =>{

        chatStore.currentChatData.tagGroups = groupNames.map(item=>{
            return new VirualGroupItem({
                name:item,
            })
        })
        if( sync ){
            // db
            const extendInfo = chatStore.currentChatData.extendInfo
            const originTags = extendInfo? (extendInfo.tagNames||[]) : [];
            // 先删除
            let delGroups = originTags.filter(item=> !groupNames.includes(item))
            console.log('delete group', delGroups)
            for (let i = 0; i < delGroups.length; i++) {
                let res = await imsdk.removeTagGroupUser(delGroups[i], userId);
            }
            // 再添加
            let addGroups = groupNames.filter(item=>!originTags.includes(item))  
            console.log('add group', addGroups)
            for (let i = 0; i < addGroups.length; i++) {
                let res = await imsdk.setTagGroupUser(addGroups[i], userId);
            }
            webIM.loadTagGroupList();
        }
        
        
    }

    @action removeTagGroup = async ( groupName: string) =>{
        let gs = this.groupList.slice().filter(item => { return item.name != groupName });
        this.setGroupList(gs);
        let res = await imsdk.removeTagGroup(groupName);
        webIM.loadTagGroupList();
    }

}

export default new TagGroupStore();
